
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';

enum StoreKey {
  privateKey(key: "privateKey"),
  mnemonic(key: "mnemonic"),
  checkMnemonic(key: "checkMnemonic"),
  publicKey(key: "publicKey"),
  token(key: "token"),
  account(key: "account"),
  isEnableFingerprintLogin(key: "isEnableFingerprintLogin"),
  isEnableFaceLogin(key: "isEnableFaceLogin"),
  isEnableGestureLogin(key: "isEnableGestureLogin"),
  localIdentityVerifyType(key: "localIdentityVerifyType"),
  sessionSecretKey(key: "sessionSecretKey"),
  /// 是否完成了用户引导页面
  isFinishGuide(key: "isFinishGuide"),
  /// 应用密码
  appPassword(key: "appPassword"),
  ;
  final String key;
  const StoreKey({required this.key});
}


/// 存储
class StoreApi {

  final storage = const FlutterSecureStorage();
  final Future<SharedPreferences> prefs =  SharedPreferences.getInstance();

  static final StoreApi _instance = StoreApi._();
  static StoreApi get instance => _instance;
  StoreApi._();

  /// 字符串
  Future<void> storeString(StoreKey key, String value){
    return prefs.then((it)=>it.setString(key.key, value));
  }

  Future<String?> takeAsString(StoreKey key) async {
    return await prefs.then((it)=>it.getString(key.key));
  }

  /// bool
  Future<void> storeBool(StoreKey key, bool value){
    return prefs.then((it)=>it.setBool(key.key, value));
  }
  Future<bool?> takeAsBool(StoreKey key) async {
    return await prefs.then((value) => value.getBool(key.key));
  }

  /// int
  Future<void> storeInt(StoreKey key, int value) async {
    return await prefs.then((it)=>it.setInt(key.key, value));
  }
  Future<int?> takeAsInt(StoreKey key) async {
    return await prefs.then((it)=>it.getInt(key.key));
  }

  /// 安全
  Future<void> secureStore(StoreKey key, String value) async {
    return await storage.write(key: key.key, value: value);
  }

  Future<String?> secureTake(StoreKey key) async {
    return await storage.read(key: key.key);
  }

}

/// 用户相关的存储API
extension UserStoreApi on StoreApi {

  /// 存储用户账号
  Future<void> storeUserAccount(String userAccount) async {
    storeString(StoreKey.account, userAccount);
  }
  Future<String?> takeUserAccount() async {
    return takeAsString(StoreKey.account);
  }

  /// 存储用户私钥
  Future<void> secureStoreUserPrivateKey(String privateKey){
    return secureStore(StoreKey.privateKey, privateKey);
  }

  Future<void> secureStoreUserPublicKey(String publicKey){
    return secureStore(StoreKey.publicKey, publicKey);
  }

  Future<String?> secureTakeUserPrivateKey(){
    return secureTake(StoreKey.privateKey);
  }
  Future<String?> secureTakeUserPublicKey(){
    return secureTake(StoreKey.publicKey);
  }

  /// 存储用户助记词
  Future<void> secureStoreUserMnemonic(List<String> mnemonic){
    return secureStore(StoreKey.mnemonic, mnemonic.join(","));
  }
  /// 获取用户助记词
  Future<List<String>?> secureTakeUserMnemonic() async {
    var res =  await secureTake(StoreKey.mnemonic);
    if(res==null){
      return null;
    }
    return res.split(",");
  }

  /// 用户是否备份了助记词
  Future<void> storeUserCheckMnemonic(bool hasCheckMnemonic){
    return secureStore(StoreKey.checkMnemonic, hasCheckMnemonic.toString());
  }

  /// 用户是否备份了助记词
  Future<bool> hasStoreUserCheckMnemonic(bool hasCheckMnemonic) async {
    return (await secureTake(StoreKey.checkMnemonic)) == true.toString();
  }

  /// 存储用户的 token
  Future<void> storeUserToken(String token){
    return secureStore(StoreKey.token, token);
  }

  Future<String?> takeUserToken(){
    return secureTake(StoreKey.token);
  }

  /// 存储用户公钥
  Future<void> storePublicKey(String publicKey){
    return storeString(StoreKey.publicKey, publicKey);
  }

  Future<String?> takePublicKey(){
    return takeAsString(StoreKey.publicKey);
  }

  /// 是否开启的指纹登录
  Future<bool?> isEnableFingerprintLogin(){
    return takeAsBool(StoreKey.isEnableFingerprintLogin);
  }

  /// 是否开启的指纹登录
  Future<void> changeFingerprintLoginStatus(bool status){
    return storeBool(StoreKey.isEnableFingerprintLogin, status);
  }

  /// save password
  Future<void> secureStoreAppPassword(String password){
    return secureStore(StoreKey.appPassword, password);
  }

  /// 获取应用密码
  Future<String?> secureTakeAppPassword(){
    return secureTake(StoreKey.appPassword);
  }

  /// 已经绑定了用户
  Future<bool> hasBindUser() async {
    var pwd = await secureTakeUserMnemonic();
    return pwd!=null;
  }

  /// login out, clear user info
  clearUser() async {
    await storage.delete(key: StoreKey.token.key);
    await storage.delete(key: StoreKey.publicKey.key);
    await storage.delete(key: StoreKey.privateKey.key);
    await storage.delete(key: StoreKey.appPassword.key);
    await storage.delete(key: StoreKey.checkMnemonic.key);
  }

}